package com.example.dao;

import com.example.bean.User;
import com.example.util.DBUtil;

import java.sql.*;

/**
 * 用户
 */
public class UserDao {
    ResultSet resultSet = null;
    Connection connection = null;
    Statement statement = null;

    // 根据用户名查询用户信息
    public User selectUserByName(User user) {
        User findUser = null;
        try {
            Connection connection = DBUtil.getConnection();
            statement = connection.createStatement();
            String sql = "SELECT * from user where username='" + user.getUsername() + "'";
            ResultSet resultSet = statement.executeQuery(sql);
            if(resultSet.next()) {
                findUser = new User();
                findUser.setId(resultSet.getString("uid"));
                findUser.setUsername(resultSet.getString("username"));
                findUser.setPassword(resultSet.getString("password"));
                findUser.setEmail(resultSet.getString("email"));
                findUser.setPhoto(resultSet.getString("photo"));
                findUser.setBirthday(resultSet.getDate("birthday"));
                findUser.setName(resultSet.getString("name"));
                findUser.setTelephone(resultSet.getString("telephone"));
                findUser.setSex(resultSet.getString("sex"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return findUser;
    }

    // 注册用户
    public int insertUser(User user) {
        int n = 0;
        try {
            connection = DBUtil.getConnection();
            PreparedStatement statement = connection.prepareStatement(
                    "insert into user values (?, ?, ?, ?, ?, ?, ?, ?, null, null)");
            statement.setString(1, user.getId());
            statement.setString(2, user.getUsername());
            statement.setString(3, user.getPassword());
            statement.setString(4, user.getName());
            statement.setString(5, user.getEmail());
            statement.setString(6, user.getTelephone());
            statement.setDate(7, new Date(user.getBirthday().getTime()));
            statement.setString(8, user.getSex());
            n = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return n;
    }

    // 更新用户信息
    public int updateUser(User u) {
        int n = 0;
        PreparedStatement pstmt = null;
        // 1.连接数据库
        try {
            connection = DBUtil.getConnection();
            // 2.使用数据库（增删改查）
            // stmt=conn.createStatement();
            String sql = "update user SET password=?,name=?,email=?,telephone=?,birthday=?,sex=?,photo=? where username=?";
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, u.getPassword());
            pstmt.setString(2, u.getName());
            pstmt.setString(3, u.getEmail());
            pstmt.setString(4, u.getTelephone());
            pstmt.setDate(5, new Date(u.getBirthday().getTime()));
            pstmt.setString(6, u.getSex());
            pstmt.setString(7, u.getPhoto());
            pstmt.setString(8, u.getUsername());
            n = pstmt.executeUpdate();
            System.out.println("n:" + n);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(resultSet, statement, connection);
        }
        return n;
    }
}
